<template>
  <div class="text-to-image-page">
    <div class="page-header">
      <h1 class="page-title">文生图</h1>
      <p class="page-description">通过输入文字描述，让AI为您生成精美的图片作品</p>
    </div>

    <div class="main-content">
      <!-- 左侧控制面板 -->
      <div class="control-panel">
        <div class="control-section">
          <h3 class="section-title">文字描述</h3>
          <div class="prompt-area">
            <textarea
              v-model="prompt"
              :disabled="isGenerating"
              placeholder="描述您想要生成的图片，例如：一只可爱的小猫坐在阳光下的花园里，周围开满了五颜六色的花朵，画风唯美，光线柔和..."
              class="prompt-textarea"
              rows="6"
            ></textarea>
          </div>
        </div>

        <div class="control-section">
          <h3 class="section-title">生成设置</h3>
          <div class="settings-group">
            <div class="setting-item">
              <label class="setting-label">图片尺寸</label>
              <div class="size-options">
                <button 
                  v-for="size in imageSizes" 
                  :key="size.id"
                  :class="['size-btn', { active: selectedSize === size.id }]"
                  @click="selectSize(size.id)"
                >
                  {{ size.name }}
                </button>
              </div>
            </div>

            <div class="setting-item">
              <label class="setting-label">艺术风格</label>
              <div class="style-grid">
                <button 
                  v-for="style in artStyles" 
                  :key="style.id"
                  :class="['style-btn', { active: selectedStyle === style.id }]"
                  @click="selectStyle(style.id)"
                >
                  <span class="style-icon">{{ style.icon }}</span>
                  <span class="style-name">{{ style.name }}</span>
                </button>
              </div>
            </div>

            <div class="setting-item">
              <label class="setting-label">创意度</label>
              <div class="slider-group">
                <input 
                  v-model="creativity" 
                  type="range" 
                  min="0" 
                  max="100" 
                  class="setting-slider"
                />
                <span class="slider-value">{{ creativity }}%</span>
              </div>
            </div>
          </div>
        </div>

        <div class="control-section">
          <div class="action-controls">
            <button 
              class="action-btn primary large" 
              @click="generateImage" 
              :disabled="!prompt.trim() || isGenerating"
            >
              {{ isGenerating ? '生成中...' : '开始生成' }}
            </button>
            <button class="action-btn" @click="clearResults" :disabled="generatedImages.length === 0">
              清空结果
            </button>
            <button class="action-btn" @click="downloadAll" :disabled="generatedImages.length === 0">
              批量下载
            </button>
          </div>
        </div>
      </div>

      <!-- 右侧预览区域 -->
      <div class="preview-area">
        <div class="result-container">
          <h3 class="result-title">生成结果</h3>
          
          <div v-if="!isGenerating && generatedImages.length === 0" class="empty-state">
            <div class="empty-icon">🎨</div>
            <p>输入描述文字，开始您的AI艺术创作之旅</p>
          </div>

          <div v-if="isGenerating" class="generating-state">
            <div class="generating-animation">
              <div class="paint-brush">🖌️</div>
              <div class="sparkles">
                <span>✨</span>
                <span>✨</span>
                <span>✨</span>
              </div>
            </div>
            <p class="generating-text">AI正在为您创作精美图片...</p>
            <div class="progress-bar">
              <div class="progress-fill" :style="{ width: progress + '%' }"></div>
            </div>
            <span class="progress-text">{{ progress }}%</span>
          </div>

          <div v-if="generatedImages.length > 0" class="images-grid">
            <div 
              v-for="(image, index) in generatedImages" 
              :key="index"
              class="image-card"
            >
              <div class="image-wrapper">
                <img :src="image" :alt="`生成图片 ${index + 1}`" class="result-image" />
                <div class="image-overlay">
                  <button class="overlay-btn" @click="downloadImage(image, index)">
                    📥 下载
                  </button>
                  <button class="overlay-btn" @click="favoriteImage(index)">
                    ❤️ 收藏
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { textToImageApi } from '#/api/core/ai';
import { message } from 'ant-design-vue';

export default {
  name: 'TextToImage',
  data() {
    return {
      prompt: '',
      isGenerating: false,
      progress: 0,
      generatedImages: [],
      selectedSize: '1:1',
      selectedStyle: 'realistic',
      creativity: 75,
      progressInterval: null,
      
      imageSizes: [
        { id: '1:1', name: '正方形' },
        { id: '16:9', name: '横屏' },
        { id: '9:16', name: '竖屏' },
        { id: '4:3', name: '经典' }
      ],
      
      artStyles: [
        { id: 'realistic', name: '写实', icon: '📷' },
        { id: 'anime', name: '动漫', icon: '🎭' },
        { id: 'oil', name: '油画', icon: '🎨' },
        { id: 'watercolor', name: '水彩', icon: '🌊' },
        { id: 'sketch', name: '素描', icon: '✏️' },
        { id: 'digital', name: '数字艺术', icon: '💻' }
      ]
    }
  },
  methods: {
    selectSize(sizeId) {
      this.selectedSize = sizeId;
    },
    
    selectStyle(styleId) {
      this.selectedStyle = styleId;
    },
    
    async generateImage() {
      if (!this.prompt.trim()) return;
      
      this.isGenerating = true;
      this.progress = 0;
      this.generatedImages = [];
      
      // 启动进度条动画
      this.progressInterval = setInterval(() => {
        this.progress += Math.random() * 15;
        if (this.progress >= 90) {
          this.progress = 90;
          clearInterval(this.progressInterval);
        }
      }, 300);
      
      try {
        // 调用API生成图片
        const result = await textToImageApi({
          prompt: this.prompt,
          size: this.selectedSize,
          style: this.selectedStyle,
          creativity: this.creativity
        });
        
        // 更新进度为100%
        clearInterval(this.progressInterval);
        this.progress = 100;
        
        // 显示生成的图片
        this.generatedImages = result.images;
        
        // 提示成功
        message.success(`成功生成${result.images.length}张图片！`);
      } catch (error) {
        // 处理错误
        message.error(`生成失败: ${error.message || '未知错误'}`);
        console.error('生成图片失败:', error);
      } finally {
        // 无论成功失败都结束生成状态
        this.isGenerating = false;
        clearInterval(this.progressInterval);
      }
    },
    
    clearResults() {
      this.generatedImages = [];
      this.prompt = '';
      this.progress = 0;
    },
    
    downloadImage(imageUrl, index) {
      const link = document.createElement('a');
      link.download = `text_to_image_${index + 1}.png`;
      link.href = imageUrl;
      link.click();
    },
    
    downloadAll() {
      this.generatedImages.forEach((image, index) => {
        setTimeout(() => {
          this.downloadImage(image, index);
        }, index * 200);
      });
    },
    
    favoriteImage(index) {
      // 收藏功能，待实现
      message.success(`已收藏图片 ${index + 1}`);
    }
  }
}
</script>

<style scoped>
.text-to-image-page {
  padding: 1.5rem;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.page-header {
  text-align: center;
  margin-bottom: 2rem;
}

.page-title {
  font-size: 2.5rem;
  font-weight: bold;
  color: white;
  margin-bottom: 1rem;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.page-description {
  font-size: 1.1rem;
  color: rgba(255, 255, 255, 0.9);
  max-width: 600px;
  margin: 0 auto;
}

.main-content {
  display: flex;
  gap: 2rem;
  max-width: 1400px;
  margin: 0 auto;
}

.control-panel {
  width: 350px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 1.5rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
  height: fit-content;
}

.control-section {
  margin-bottom: 2rem;
}

.section-title {
  font-size: 1.2rem;
  font-weight: 600;
  color: #2d3748;
  margin-bottom: 1rem;
}

.prompt-textarea {
  width: 100%;
  padding: 1rem;
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  font-size: 0.95rem;
  line-height: 1.6;
  resize: vertical;
  transition: all 0.3s ease;
  background: white;
}

.prompt-textarea:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.settings-group {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.setting-item {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.setting-label {
  font-size: 0.9rem;
  font-weight: 500;
  color: #4a5568;
}

.size-options {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.5rem;
}

.size-btn {
  padding: 0.75rem;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  background: white;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 0.875rem;
  font-weight: 500;
}

.size-btn:hover {
  border-color: #667eea;
  background-color: #f7fafc;
}

.size-btn.active {
  border-color: #667eea;
  background-color: #ebf4ff;
  color: #3182ce;
}

.style-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.5rem;
}

.style-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.75rem;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  background: white;
  cursor: pointer;
  transition: all 0.3s ease;
  gap: 0.25rem;
}

.style-btn:hover {
  border-color: #667eea;
  background-color: #f7fafc;
}

.style-btn.active {
  border-color: #667eea;
  background-color: #ebf4ff;
  color: #3182ce;
}

.style-icon {
  font-size: 1.25rem;
}

.style-name {
  font-size: 0.8rem;
  font-weight: 500;
}

.slider-group {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.setting-slider {
  flex: 1;
  height: 6px;
  border-radius: 3px;
  background: #e2e8f0;
  outline: none;
}

.slider-value {
  font-size: 0.875rem;
  font-weight: 600;
  color: #667eea;
  min-width: 45px;
}

.action-controls {
  display: grid;
  gap: 0.75rem;
}

.action-btn {
  padding: 0.875rem 1rem;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.action-btn.primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.action-btn.primary.large {
  padding: 1rem 1.5rem;
  font-size: 1.1rem;
}

.action-btn.primary:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(102, 126, 234, 0.4);
}

.action-btn:not(.primary) {
  background: rgba(255, 255, 255, 0.9);
  color: #4a5568;
  border: 1px solid #e2e8f0;
}

.action-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.preview-area {
  flex: 1;
}

.result-container {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 2rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

.result-title {
  font-size: 1.25rem;
  font-weight: 600;
  color: #2d3748;
  margin-bottom: 1.5rem;
  text-align: center;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 400px;
  color: #a0aec0;
}

.empty-icon {
  font-size: 4rem;
  margin-bottom: 1rem;
}

.generating-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 400px;
  gap: 1.5rem;
}

.generating-animation {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.paint-brush {
  font-size: 3rem;
  animation: bounce 2s ease-in-out infinite;
}

.sparkles {
  display: flex;
  gap: 0.5rem;
}

.sparkles span {
  font-size: 1.5rem;
  animation: twinkle 1.5s ease-in-out infinite;
}

.sparkles span:nth-child(2) { animation-delay: 0.3s; }
.sparkles span:nth-child(3) { animation-delay: 0.6s; }

@keyframes bounce {
  0%, 20%, 50%, 80%, 100% { transform: translateY(0); }
  40% { transform: translateY(-10px); }
  60% { transform: translateY(-5px); }
}

@keyframes twinkle {
  0%, 100% { opacity: 0.3; transform: scale(0.8); }
  50% { opacity: 1; transform: scale(1.2); }
}

.generating-text {
  font-weight: 600;
  color: #2d3748;
}

.progress-bar {
  width: 300px;
  height: 8px;
  background: #e2e8f0;
  border-radius: 4px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #667eea, #764ba2);
  transition: width 0.3s ease;
}

.progress-text {
  font-size: 0.9rem;
  font-weight: 600;
  color: #667eea;
}

.images-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 1.5rem;
}

.image-card {
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.image-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
}

.image-wrapper {
  position: relative;
}

.result-image {
  width: 100%;
  height: 250px;
  object-fit: cover;
  display: block;
}

.image-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.image-card:hover .image-overlay {
  opacity: 1;
}

.overlay-btn {
  padding: 0.5rem 1rem;
  background: rgba(255, 255, 255, 0.9);
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 500;
  transition: all 0.3s ease;
}

.overlay-btn:hover {
  background: white;
  transform: scale(1.05);
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .main-content {
    flex-direction: column;
  }
  
  .control-panel {
    width: 100%;
  }
}

@media (max-width: 768px) {
  .page-title {
    font-size: 2rem;
  }
  
  .style-grid {
    grid-template-columns: repeat(3, 1fr);
  }
  
  .images-grid {
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  }
}
</style> 
